package com.huaxin.hxcloud.find;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.View;

import com.huaxin.hxcloud.R;
import com.huaxin.hxcloud.find.view.BaseListFragment;
import com.huaxin.hxcloud.find.view.ListFragment;

import java.util.ArrayList;

/**
 * Created by hebing on 2017/4/8.
 */

public class ViewpagerAdapter extends FragmentStatePagerAdapter {

    public  ArrayList<BaseListFragment> fragments;
    public  Context                     mContext;
    private String[]                    titles;
    private int                         type;
    /**
     * 0 = 全部的问题
     * 1 = 同企业用户的
     * 2 = 好友的
     * 3 = 自己收藏的
     * 4 = 关注的用户的
     * 5 = 自己的
     * 6 = 行业(圈子)
     */
    private final static int SELECT_LAST_TAB = 5;

    public ViewpagerAdapter(FragmentManager fm, Context context, int type) {
        super(fm);
        this.type = type;
        mContext = context;
        initFragments();
    }

    public ArrayList<BaseListFragment> getFragments() {
        return fragments;
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    private void initFragments() {
        fragments = new ArrayList<>();

        if (type == 2) {
            titles = new String[]{mContext.getResources().getString(R.string.test_1),
                    mContext.getResources().getString(R.string.test_2),
                    mContext.getResources().getString(R.string.test_3),
                    mContext.getResources().getString(R.string.test_5),
            };
            for (int i = 0; i < titles.length; i++) {
                if (i == (titles.length - 1)) {
                    ListFragment item = ListFragment.newInstance(SELECT_LAST_TAB, i);
                    fragments.add(item);
                } else {
                    ListFragment item = ListFragment.newInstance(i, i);
                    fragments.add(item);
                }
            }
        } else {
            titles = new String[]{mContext.getResources().getString(R.string.test_1),
                    mContext.getResources().getString(R.string.test_3),
                    mContext.getResources().getString(R.string.test_5),
            };
            for (int i = 0; i < titles.length + 1; i++) {
                if (i == 1)
                    continue;
                if (i == (titles.length)) {
                    ListFragment item = ListFragment.newInstance(SELECT_LAST_TAB, i);
                    fragments.add(item);
                } else {
                    ListFragment item = ListFragment.newInstance(i, i);
                    fragments.add(item);
                }
            }
        }
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }

    public View getSlidableView(int index) {
        return fragments.get(index).getSlidableView();
    }


}
